package com.fzy.test.leetcode352;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        Main test = new Main();
        test.longestAlternatingSubarray(new int[]{2,8},4);
        test.longestAlternatingSubarray(new int[]{3,2,5,4},5);
        test.longestAlternatingSubarray(new int[]{2,2},18);
        test.longestAlternatingSubarray(new int[]{4},1);
        test.longestAlternatingSubarray(new int[]{1,10,5},9);
        test.longestAlternatingSubarray(new int[]{4,3,1},4);
    }

    /**
    * @author LazyCat
    * @date  2023/7/2
    * @param
    * @return
     * [3, 2, 5, 4]
     * 5
     * [1,2]
     * 2
     * [2,3,4,5]
     * 4
     * [1,10,5]
     * 9
     * [2,2]
     * 18
     * [4]
     * 1
     * [4,3,1]
     * 4
    */
    public int longestAlternatingSubarray(int[] nums, int threshold) {

        int n = nums.length;
        int ans = 0;

        for(int l = 0;l<n;l++){
            int res = 0;
            for(int r = l;r<n;r++){
                if(r == l){
                    if(nums[l]%2 == 0 &&nums[l]<=threshold){
                        res++;
                    }else {
                        ans = Math.max(res,ans);
                        break;
                    }
                }else{
                    if(nums[l]%2 == 0 && nums[r]%2 !=nums[r-1]%2 && nums[r] <= threshold){
                        res++;
                    }else {
                        ans = Math.max(res,ans);
                        break;
                    }
                }
            }
            ans = Math.max(res,ans);
        }
        return ans;
    }

    public List<List<Integer>> findPrimePairs(int n) {

        boolean[] isPrime = new boolean[n+1];
        Arrays.fill(isPrime, true);
        List<List<Integer>> ans = new ArrayList<>();

        for (int i = 2; i * i <= n; i++) {
            if (isPrime[i]) {
                for (int j = i * i; j <= n; j += i) {
                    isPrime[j] = false;
                }
            }
        }

        for (int i = 2; i <= n; i++) {
            if (isPrime[i] && isPrime[n-i] && i<n-i) {
                List<Integer> primes = new ArrayList<>();
                primes.add(i);
                primes.add(n-i);
                ans.add(primes);
            }
        }

        return ans;
    }

}
